package com.zyc.studentstudy.utils

import android.text.TextUtils
import java.math.BigDecimal
import java.math.RoundingMode
import java.text.DecimalFormat
import java.text.DecimalFormatSymbols
import java.util.Locale

/**
 * Created by zhangyuncai on 2019/4/11.
 * 解决精度问题
 */
object NumberUtil {

    private val threelastDF = DecimalFormat("#0.000")
    private val twolastDF = DecimalFormat("#0.00")
    private val onelastDF = DecimalFormat("#0.0")
    private val IntlastDF = DecimalFormat("#0")

    init {
        val decimalFormatSymbols = DecimalFormatSymbols.getInstance(Locale.ENGLISH)
        decimalFormatSymbols.decimalSeparator = '.'
        onelastDF.decimalFormatSymbols = decimalFormatSymbols
        twolastDF.decimalFormatSymbols = decimalFormatSymbols
        IntlastDF.decimalFormatSymbols = decimalFormatSymbols
        threelastDF.decimalFormatSymbols = decimalFormatSymbols
    }

    @JvmStatic
    fun getDecimalFormat(): DecimalFormat {
        val df = DecimalFormat()
        val decimalFormatSymbols = DecimalFormatSymbols.getInstance(Locale.ENGLISH)
        decimalFormatSymbols.decimalSeparator = '.'
        df.decimalFormatSymbols = decimalFormatSymbols
        return df
    }

    @JvmStatic
    fun getDecimalFormat(pattern: String): DecimalFormat {
        val df = DecimalFormat(pattern)
        val decimalFormatSymbols = DecimalFormatSymbols.getInstance(Locale.ENGLISH)
        decimalFormatSymbols.decimalSeparator = '.'
        df.decimalFormatSymbols = decimalFormatSymbols
        return df
    }

    @JvmStatic
    fun equalsHex(hex1: Int, hex2: String): Boolean {
        return equalsHex(hex1, String.format("%02X", hex1))
    }

    @JvmStatic
    fun equalsHex(hex1: String, hex2: Int): Boolean {
        return equalsHex(hex1, String.format("%02X", hex2))
    }

    @JvmStatic
    fun equalsHex(hex1: String, hex2: String): Boolean {
        if (TextUtils.isEmpty(hex1) || TextUtils.isEmpty(hex2)) {
            return false
        }
        return equalsHex(parseInteger(hex1, 16), parseInteger(hex2, 16))
    }

    @JvmStatic
    fun equalsHex(hex1: Int, hex2: Int): Boolean {
        return hex1 == hex2
    }

    @JvmStatic
    fun parseFloat(strIn: String?): Float {
        var l = 0f
        if (strIn.isNullOrEmpty()) {
            return l
        }
        try {
            l = strIn.toFloat()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return l
    }

    @JvmStatic
    fun parseInt(strIn: String?): Int {
        return parseInteger(strIn)
    }

    @JvmStatic
    fun parseInteger(strIn: String?): Int {
        var l = 0
        if (strIn.isNullOrEmpty()) {
            return l
        }
        try {
            l = strIn.toInt()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return l
    }

    @JvmStatic
    fun parseInteger(strIn: String?, hex: Int): Int {
        var l = 0
        if (strIn.isNullOrEmpty()) {
            return l
        }
        try {
            l = strIn.toInt(hex)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return l
    }

    @JvmStatic
    fun parseLong(strIn: String?): Long {
        var l = 0L
        if (strIn.isNullOrEmpty()) {
            return l
        }
        try {
            l = strIn.toLong()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return l
    }

    @JvmStatic
    fun parseLong(strIn: String?, hex: Int): Long {
        var l = 0L
        if (strIn.isNullOrEmpty()) {
            return l
        }
        try {
            l = strIn.toLong(hex)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return l
    }

    @JvmStatic
    fun parseDouble(strIn: String?): Double {
        var l = 0.0
        if (strIn.isNullOrEmpty()) {
            return l
        }
        try {
            l = strIn.toDouble()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return l
    }

    private fun removeChar(inputStr: String): String {
        return inputStr.replace(",", "")
    }

    @JvmStatic
    fun add(v1: String, v2: String): String {
        var add: String? = null
        try {
            val b1 = BigDecimal(removeChar(v1))
            val b2 = BigDecimal(removeChar(v2))
            add = b1.add(b2).toString()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return add ?: ""
    }

    @JvmStatic
    fun subtract(v1: String, v2: String): String {
        var subtract: String? = null
        try {
            val b1 = BigDecimal(removeChar(v1))
            val b2 = BigDecimal(removeChar(v2))
            subtract = b1.subtract(b2).toString()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return subtract ?: ""
    }

    @JvmStatic
    fun multiply(v1: String?, v2: String?): String {
        if (v1.isNullOrEmpty() || v2.isNullOrEmpty()) {
            return "0"
        }
        var multiply: String? = null
        try {
            val b1 = BigDecimal(removeChar(v1))
            val b2 = BigDecimal(removeChar(v2))
            multiply = b1.multiply(b2).toString()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return multiply ?: ""
    }

    @JvmStatic
    fun divide(d1: Any, d2: Any): Double {
        val b1 = BigDecimal(d1.toString())
        val b2 = BigDecimal(d2.toString())
        return b1.divide(b2, 2, BigDecimal.ROUND_HALF_UP).toDouble()
    }

    @JvmStatic
    fun multiply(d1: Any, d2: Any): Double {
        val b1 = BigDecimal(d1.toString())
        val b2 = BigDecimal(d2.toString())
        return b1.multiply(b2).toDouble()
    }

    @JvmStatic
    fun multiply(d1: Any, d2: Any, d3: Any): Double {
        val b1 = BigDecimal(d1.toString())
        val b2 = BigDecimal(d2.toString())
        val b3 = BigDecimal(d3.toString())
        return b1.multiply(b2).multiply(b3).toDouble()
    }

    @JvmStatic
    fun threelastDF(d: Double): String {
        threelastDF.roundingMode = RoundingMode.HALF_UP
        return threelastDF.format(BigDecimal(d.toString()))
    }

    @JvmStatic
    fun threelastDouble(d: Double): Double {
        return parseDouble(threelastDF(d))
    }

    @JvmStatic
    fun twolastDF(d: Double): String {
        twolastDF.roundingMode = RoundingMode.HALF_UP
        return twolastDF.format(BigDecimal(d.toString()))
    }

    @JvmStatic
    fun twolastDouble(d: Double): Double {
        return parseDouble(twolastDF(d))
    }

    @JvmStatic
    fun onelastDF(d: Double): String {
        onelastDF.roundingMode = RoundingMode.HALF_UP
        return onelastDF.format(BigDecimal(d.toString()))
    }

    @JvmStatic
    fun onelastDouble(d: Double): Double {
        return parseDouble(onelastDF(d))
    }

    @JvmStatic
    fun IntlastText(d: Double): String {
        IntlastDF.roundingMode = RoundingMode.HALF_UP
        return IntlastDF.format(BigDecimal(d.toString()))
    }

    @JvmStatic
    fun Intlast(d: Double): Int {
        return parseInteger(IntlastText(d))
    }
}